/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package technotheist.data.wrap;

import gnu.trove.map.hash.TIntObjectHashMap;
import technotheist.data.BBuffer;
import technotheist.data.BVec;
import technotheist.data.BVec.Size;

//

public class VecBuffer<N extends Number> extends BBuffer<BVec<N>> {
    private final BBuffer<N> src;
    private final Size vecSize;
    private final int bSize;
    private final TIntObjectHashMap<BVec<N>> vecs;

    public VecBuffer(BBuffer<N> src, Size vecSize, int bSize) {
	this.src = src;
	this.vecSize = vecSize;
	this.bSize = bSize;
	this.vecs = new TIntObjectHashMap<>();
    }

    public BBuffer<N> src() {
	return src;
    }

    @Override
    public int size() {
	return vecSize.elems;
    }

    @Override @SuppressWarnings("unchecked")
    public BVec<N> s(int index) {
	BVec<N> tmp = vecs.get(index);
	if (tmp == null) {
	    tmp = BVec.createVec((VecBuffer) src, index, vecSize);
	    vecs.put(index, tmp);
	}
	return tmp;
    }

    @Override 
    public void s(int index, BVec<N> v) {
	BVec<N> tmp = vecs.get(index);
	if (tmp == null) {
	    tmp = v;
	    vecs.put(index, tmp);
	}
	v.set(v);
    }

    @Override
    public Class<?> primType() {
	return src.primType();
    }
    
}
